package me.leiusual.sharing.ui.account

import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.Transformations
import androidx.lifecycle.ViewModel
import me.leiusual.sharing.api.model.SendSMS
import me.leiusual.sharing.repository.UserRepository
import me.leiusual.sharing.repository.model.Data
import me.leiusual.sharing.ui.account.model.ParamsPasswordReset
import me.leiusual.sharing.util.extension.toLiveData
import javax.inject.Inject

/**
 * Create by leiusual on 2018/7/16
 */
class AccountForgetViewModel @Inject constructor(
    private val userRepository: UserRepository
) : ViewModel() {

  private val _sendSMS = MutableLiveData<String>()
  val sendSMS: LiveData<Data<Unit>> = Transformations.switchMap(_sendSMS) {
    userRepository.sendSMS(SendSMS.Type.RESET_PASSWORD, it).toLiveData()
  }

  fun sendSMS(phone: String) {
    _sendSMS.value = phone
  }

  private val _passwordReset = MutableLiveData<ParamsPasswordReset>()
  val passwordReset: LiveData<Data<Unit>> = Transformations.switchMap(_passwordReset) {
    userRepository.passwordReset(it).toLiveData()
  }

  fun passwordReset(mobile: String, username: String, newPassword: String, checkCode: String) {
    _passwordReset.value = ParamsPasswordReset(mobile, username, newPassword, checkCode)
  }
}